// Copyright (c) Huawei Technologies Co., Ltd. 2025. All rights reserved.
// This source file is part of the Cangjie project, licensed under Apache-2.0
// with Runtime Library Exception.
//
// See https://cangjie-lang.cn/pages/LICENSE for license information.

#include "Actions/Action.h"

namespace CangjieMigrator::Actions {

template <typename>
constexpr bool ALWAYS_FALSE_VALUE = false;

KindType GetKind(const Action &action)
{
    return std::visit([](const auto &variant) -> KindType {
            using T = std::decay_t<decltype(variant)>;
            if constexpr (std::is_same_v<T, Replace>) {
                return KindType::Replace;
            } else if constexpr (std::is_same_v<T, Comment>) {
                return KindType::Comment;
            } else {
                static_assert(ALWAYS_FALSE_VALUE<T>, "Unhandled Action type in GetKind()");
                throw std::logic_error("Unhandled Action type in GetKind()");
            }
        }, action);
}

std::string ToString(const Action &action)
{
    return std::visit([](const auto &a) -> std::string {
            return a.ToString();
        }, action);
}

PositionRange GetRange(const Action &action)
{
    auto result = PositionRange::Empty();
    Dispatch(action,
        [&result](const Replace &replace) { result = replace.range; },
        [&result](const Comment &comment) { result = comment.range; }
    );
    return result;
}

} // namespace CangjieMigrator::Actions
